package com.example.administrator.shihuo.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.shihuo.R;
import com.example.administrator.shihuo.activities.LoginActivity;
import com.example.administrator.shihuo.adapters.MimelistViewAdapter;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by Sunshine on 2016/5/11.
 */
public class MyFragment extends Fragment {
    private Handler handler = new Handler();
    private View rootView;
    private String[] names1 = new String[]{"购物车", "我的订单", "我的钱包", "我的收藏", "我的晒物", "我赞过的商品", "收货地址"};
    private String[] names2 = new String[]{"客服", "设置", "关于版本"};
    private ListView listView;
    private MimelistViewAdapter adapter;
    private Button loginBtn;
    private ImageView logoImageView;
    private TextView nicknameTextView;
    private SharedPreferences sharedPreferences;
    private Button LoginBtn;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(getActivity());
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_my_layout, container, false);
        initView();
        initData();
        setData();
        return rootView;
    }

    @Subscribe
    public void getIcon(Bundle bundle) {
        String userIcon = bundle.getString("userIcon");
        ImageOptions options = new ImageOptions.Builder().setCircular(true).build();
        x.image().bind(logoImageView, userIcon, options);

        final String userName = bundle.getString("userName");
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        nicknameTextView.setText(userName);
                    }
                });
            }
        }).start();
//        nicknameTextView.setText(userName);
    }

    @Subscribe
    public void getBoolean(Boolean flag) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginBtn.setVisibility(View.GONE);
                        LoginBtn.setVisibility(View.VISIBLE);
                    }
                });
            }
        }).start();

    }

    private void initData() {
        sharedPreferences = getActivity().getSharedPreferences("isLogin", Context.MODE_PRIVATE);
        adapter = new MimelistViewAdapter(getActivity(), names1, names2);
    }

    private void setData() {
        listView.setAdapter(adapter);
        String userIcon = sharedPreferences.getString("userIcon", null);
        if (userIcon != null) {
            ImageOptions options = new ImageOptions.Builder().setCircular(true).build();
            x.image().bind(logoImageView, userIcon, options);
        }
        String userName = sharedPreferences.getString("userName", null);
        if (userName != null) {
            nicknameTextView.setText(userName);
        }
        if (sharedPreferences.getBoolean("isLogin", false) == false) {
            loginBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //第三方分享；
                    // showShare();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            });
        } else {
            loginBtn.setVisibility(View.GONE);
            LoginBtn.setVisibility(View.VISIBLE);
        }
    }

    private void initView() {
        listView = (ListView) rootView.findViewById(R.id.list1);
        loginBtn = (Button) rootView.findViewById(R.id.btn_myfragment_login);
        LoginBtn = (Button) rootView.findViewById(R.id.btn_myfragment_LOGIN);
        logoImageView = (ImageView) rootView.findViewById(R.id.profile_image_url);
        nicknameTextView = (TextView) rootView.findViewById(R.id.tv_nickname);
    }

    //分享文本；
    private void showShare() {
        ShareSDK.initSDK(getActivity());
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        //oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(getContext());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
